import 'package:flutter/material.dart';
import '../../resources/l10n/kq_intl.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../listener/kq_tap_gesture_listener.dart';

///该view提供判断是否有权限时按钮的透明度与无权限时点击的提示
class KqPermissionView extends StatelessWidget {
  /// 是否有权限
  final bool hasPermission;

  /// 当没有权限时的提示语
  final String? noPermissionNotice;

  /// 子view
  final Widget? child;

  /// 当没有权限时，子view的透明度
  final double noPermissionAlpha;

  /// 当没有权限是，子view点击事件是否可用，默认不可用
  final bool noPermissionEnable;

  const KqPermissionView({
    super.key,
    this.hasPermission = true,
    this.noPermissionNotice,
    this.noPermissionAlpha = 0.45,
    required this.child,
    this.noPermissionEnable = false,
  });

  @override
  Widget build(BuildContext context) {
    return hasPermission || noPermissionEnable
        ? KqTapGestureListener(
            onTap: () {
              if (!hasPermission) {
                KqToast.showNormal(
                    noPermissionNotice ?? KqIntl.currentResource.noPermission);
              }
            },
            child: Opacity(
              opacity: hasPermission ? 1.0 : noPermissionAlpha,
              child: child,
            ),
          )
        : KqInkWell(
            enableRipple: false,
            onTap: () {
              if (!hasPermission) {
                KqToast.showNormal(
                    noPermissionNotice ?? KqIntl.currentResource.noPermission);
              }
            },
            child: Opacity(
              opacity: hasPermission ? 1.0 : noPermissionAlpha,
              child: IgnorePointer(
                child: child,
              ),
            ),
          );
  }
}
